//
//  LYZRecommendTagsViewController.m
//  01-百思不得姐
//
//  Created by 李昱铮 on 16/8/29.
//  Copyright © 2016年 LYZ. All rights reserved.
//

#import "LYZRecommendTagsViewController.h"
#import "LYZRecommendTag.h"
#import <AFNetworking.h>
#import <SVProgressHUD.h>
#import <MJExtension.h>
#import "LYZRecommendTagCell.h"

@interface LYZRecommendTagsViewController ()
/** 标签数据 */
@property (nonatomic,strong) NSArray * tags;

@end

static NSString * const LYZTagsId = @"tag";

@implementation LYZRecommendTagsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupTableView];
    
    [self loadTags];
}

- (void)setupTableView
{
    self.title = @"推荐标签";
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([LYZRecommendTagCell class]) bundle:nil] forCellReuseIdentifier:LYZTagsId];
    
    self.tableView.rowHeight = 70;
    self.tableView.separatorStyle = UITableViewCellAccessoryNone;
    self.tableView.backgroundColor = LYZGlobaBg;
}

- (void)loadTags
{
    //发送请求给服务器
    [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeBlack];
    
    //请求参数
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"a"] = @"tag_recommend";
    params[@"action"] = @"sub";
    params[@"c"] = @"topic";
    
    //发送请求
    [[AFHTTPSessionManager manager] GET:@"http://api.budejie.com/api/api_open.php" parameters:params success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        self.tags = [LYZRecommendTag objectArrayWithKeyValuesArray:responseObject];
        [self.tableView reloadData];
        
        [SVProgressHUD dismiss];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [SVProgressHUD showErrorWithStatus:@"加载数据失败!"];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tags.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LYZRecommendTagCell *cell = [tableView dequeueReusableCellWithIdentifier:LYZTagsId];
    
    cell.recommendTag = self.tags[indexPath.row];
    
    return cell;
}
@end
